package gui.applets;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class BusinessLogic {
	private int modifier;

	BusinessLogic(int mod) {
		this.setModifier(mod);
	}

	public int getModifier() {
		return modifier;
	}

	public void setModifier(int modifier) {
		this.modifier = modifier;
	}

	public int calculation(int value) {
		return value * modifier;
	}

	public int calculation2(int value) {
		return value + modifier;
	}

}

public class AppletBusiness extends Applet {

	TextField tf = new TextField(20);
	TextField mod = new TextField(20);

	BusinessLogic b1 = new BusinessLogic(2);

	Button btn1 = new Button("Calc 1");
	Button btn2 = new Button("Calc 2");

	public void init() {
		this.add(this.tf);
		this.btn1.addActionListener(new Calc1());
		this.btn2.addActionListener(new Calc2());
		this.add(this.btn1);
		this.add(this.btn2);

		this.mod.addTextListener(new ModL());
		this.add(new Label("Modifier"));
		this.add(mod);
	}

	static int getValue(TextField tfs) {
		return Integer.parseInt(tfs.getText());
	}

	class Calc1 implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			tf.setText(Integer.toString(b1.calculation(getValue(tf))));

		}

	}

	class Calc2 implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			tf.setText(Integer.toString(b1.calculation2(getValue(tf))));
		}

	}

	class ModL implements TextListener {

		@Override
		public void textValueChanged(TextEvent e) {
			b1.setModifier(getValue(mod));
		}

	}

	public static void main(String[] args) {
		AppletBusiness ab = new AppletBusiness();
		Frame frame = new Frame("Business");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		frame.add(ab, BorderLayout.CENTER);
		frame.setSize(200, 200);
		ab.init();
		ab.start();
		frame.setVisible(true);
	}

}
